import { Component } from '@angular/core';

import { NavController, LoadingController } from 'ionic-angular';
import {SearchService} from "../../services/search.service";
import {XinwenList} from "../xinwen-list/xinwen-list";


@Component({
  templateUrl: 'xinwen.html',
  providers: [SearchService]
})
export class XinWen {


  public items: Array<{
    imgurl: string,
    date: string,
    title: string,
    url:any,
  }>;


  constructor(
    public navCtrl: NavController,
    public loadingCtrl: LoadingController,
    public service:SearchService,
  ){
    this.getTianQi('top');
  }


  getTianQi(tpyes){
    this.showLoading('加载中');
    this.service.getWar(tpyes).subscribe(
      data => {
        this.items = [];
        var datas =data.result.data;
        console.log(datas.length);
        for (let i=0; i < datas.length; i++){
          this.items.push({
            imgurl: datas[i].thumbnail_pic_s,
            date: datas[i].date,
            title: datas[i].title,
            url: datas[i].url,
          });
        }
        this.hideLoading();

        let odiv=document.getElementById("divid");
        let oa  =odiv.getElementsByTagName("a");
        for (let i=0; i<oa.length; i++){
          oa[i].onclick=function () {
            for (let i=0; i<oa.length; i++){
              oa[i].style.color = "#505050";
            };
            this.style.color = "#f85959";
          }
        }

      },
      error => {
        console.log('get失败');
        this.hideLoading();
      }
    );

    }

  itemSelected(p){
    this.navCtrl.push(XinwenList, {
      p:p
    });
  }

  public loading;

  showLoading(message) {
    this.loading = this.loadingCtrl.create({
      content: message,
      duration: 2000
    });
    this.loading.present();
  }

  hideLoading() {
    if (this.loading != null) {
      this.loading.dismissAll();
    }
  }

  showToast(message: string) {
    let toast = this.loadingCtrl.create({
      duration: 1000,
    });
    toast.present();
  }


}
